package com.Location;


import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.util.Log;
import com.Data.GeoTag;
import com.Managers.UserManagerClass;


public class Async_GeoUpdate extends AsyncTask<PictureLocation, String, GeoTag> {

    private static final String TAG = "USERGEOLOCATION";

    private final Context context;

    private final Location location;

    public Async_GeoUpdate(Context context, Location location) {

        super();
        this.context = context;
        this.location = location;
    }

    public GeoTag getNames(Context context, Double latitude, Double longitude) {

        GeoTag gtag = new GeoTag();
        try {
            if (context != null) {
                Geocoder geocoder;
                List<Address> addresses;
                geocoder = new Geocoder(context, Locale.ENGLISH);
                //
                addresses = geocoder.getFromLocation(latitude, longitude, 1);
                //
                if (addresses.size() > 0) {
                    String address = addresses.get(0).getFeatureName(); // addresses.get(0).getAddressLine(0);
                    String city = addresses.get(0).getLocality(); // addresses.get(0).getAddressLine(1);
                    String country = addresses.get(0).getCountryName(); // addresses.get(0).getAddressLine(2);
                    //
                    gtag.setAddress(address);
                    gtag.setCity(city);
                    gtag.setCountry(country);
                    gtag.setLatitude(latitude);
                    gtag.setLongitude(longitude);

                }
                return gtag;
            }
            else {
                GeoTag gt = new GeoTag();
                return gt;
            }
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage());
            return gtag;
        }
    }

    @Override
    protected GeoTag doInBackground(PictureLocation... pl) {

        Double lat, lon;
        //
        // LocationManager locationManager = (LocationManager)
        // context.getSystemService(Context.LOCATION_SERVICE);
        // Criteria criteria = new Criteria();
        // String bestProvider = locationManager.getBestProvider(criteria,
        // false);
        // location = locationManager.getLastKnownLocation(bestProvider);
        // //
        if (location != null) {
            lat = location.getLatitude();
            lon = location.getLongitude();
        }
        else {
            lat = (double) 0;
            lon = (double) 0;
        }
        //
        return getNames(context, lat, lon);
    }

    @Override
    protected void onPostExecute(GeoTag result) {

        UserManagerClass.UserLocation = result;
        Log.e(TAG, "Location Updated, ure: " + result.toString());
    }

}
